package com.aaa.controller;

import com.aaa.pojo.MoneyRecord;
import com.aaa.pojo.MyLoan;
import com.aaa.service.MoneyRecordService;
import com.aaa.service.MyLoanService;
import com.aaa.tool.Result;
import com.aaa.tool.SnowflakeIdWorker;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;

@Controller
@SessionAttributes
@ResponseBody
public class MyLoanController {
    @Autowired
    private MyLoanService myLoanService;
    @Autowired
    private MoneyRecordService moneyRecordService;

    private SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker(10,30);

    @RequestMapping("/myLoanAdd")
    public Result insert(HttpSession session, MyLoan myLoan){
        //生成唯一id
        myLoan.setMl_id(snowflakeIdWorker.nextId()+"");
        //获取用户id
        int uid = (Integer)session.getAttribute("uid");
        myLoan.setU_id(uid);
        myLoanService.insert(myLoan);
        return Result.ok();
    }

    // 查询 网贷信息 分页
    @RequestMapping("/moneyRecord/{currentPage}/{pageSize}")
    public PageInfo<MoneyRecord> getChangePage(@PathVariable(name = "currentPage") int currentPage, @PathVariable(name = "pageSize") int pageSize){
        PageInfo<MoneyRecord> changeByPages = moneyRecordService.getChangeByPages(currentPage, pageSize);
        return changeByPages;
    }


}
